package com.website.view.menu
{
    import com.greensock.*;
    import com.website.control.PathManager;
    import com.website.util.Constant;
    import com.website.view.menu.NavItem;
    import com.website.view.menu.NavMenu;
    
    import flash.display.*;
    import flash.events.*;

    public class DatePaginatedMenu extends Sprite
    {
        var initShell:Boolean;
        var myXMLList:XMLList;
        var btnsArray:Array;
        var ellipseArray:Array;
        var navMenu:NavMenu;
        public var menuType:String;
        public var currNavId:int;
        var rArrow:Sprite;
        var visBtnsArray:Array;
        var navItem:NavItem;
        var ellipse:NavItem;
        var lArrow:Sprite;

        public function DatePaginatedMenu(nameStr:String, xmlList:XMLList) : void
        {
            trace("DATE PAGINATED");
            menuType = "DatePaginatedMenu";
            visible = false;
            alpha = 0;
            myXMLList = xmlList;
            currNavId = 0;
            initShell = false;
            this.name = nameStr;
            btnsArray = new Array();
            buildCol(myXMLList);
//            Constant.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            trace("----------------------------------------------");
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, Constant.fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < btnsArray.length)
            {
                
                var _loc_2:* = Constant.unitW / 75;
                btnsArray[_loc_1].field.scaleY = Constant.unitW / 75;
                btnsArray[_loc_1].field.scaleX = _loc_2;
                btnsArray[_loc_1].bg.width = Constant.unitW;
                btnsArray[_loc_1].bg.height = btnsArray[_loc_1].field.height;
                btnsArray[_loc_1].x = 0;
                btnsArray[_loc_1].y = Math.round(14 * _loc_1 * Constant.unitW / 75);
                _loc_1++;
            }
            return;
        }

        function buildCol(param1:XMLList) : void
        {
            var _loc_2:* = undefined;
            var _loc_3:* = undefined;
            var _loc_4:* = undefined;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            _loc_2 = 1;
            _loc_3 = 1;
            _loc_4 = param1[0].@label.substring(2, 4);
            _loc_5 = param1[0].@label.substring(2, 4);
            navItem = new NavItem( _loc_5, "", 0);
            navItem.name = "navItem0";
            navItem.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
            navItem.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
            navItem.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
            addChild(navItem);
            btnsArray.push(navItem);
            _loc_6 = 0;
            while (_loc_6 < param1.length())
            {
                
                if (param1[_loc_6].item.length() > 0)
                {
                    _loc_4 = param1[_loc_6].@label.substring(2, 4);
                    _loc_2 = _loc_2 + 1;
                    if (_loc_2 == 71)
                    {
                        _loc_2 = 1;
                        _loc_5 = _loc_5 + (" - " + param1[_loc_6].@label.substring(2, 4));
                        btnsArray[(btnsArray.length - 1)].field.text = _loc_5;
                        _loc_5 = _loc_4;
                        navItem = new NavItem( _loc_5, "", btnsArray.length);
                        navItem.name = "navItem" + btnsArray.length;
                        navItem.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
                        navItem.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
                        navItem.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                        addChild(navItem);
                        btnsArray.push(navItem);
                    }
                    _loc_7 = 0;
                    while (_loc_7 < param1[_loc_6].item.length())
                    {
                        
                        _loc_2 = _loc_2 + 1;
                        if (_loc_2 == 71)
                        {
                            _loc_2 = 1;
                            if (_loc_7 == (param1[_loc_6].item.length - 1))
                            {
                                if (param1[(_loc_6 + 1)] != undefined)
                                {
                                    _loc_5 = _loc_5 + (" - " + param1[(_loc_6 + 1)].@label.substring(2, 4));
                                }
                                else
                                {
                                    _loc_5 = _loc_5 + (" - " + param1[_loc_6].@label.substring(2, 4));
                                }
                            }
                            else
                            {
                                _loc_5 = _loc_5 + (" - " + param1[_loc_6].@label.substring(2, 4));
                            }
                            btnsArray[(btnsArray.length - 1)].field.text = _loc_5;
                            if (_loc_6 != (param1.length() - 1) && _loc_7 != (param1[_loc_6].item.length() - 1))
                            {
                                if (_loc_7 == (param1[_loc_6].item.length - 1))
                                {
                                    if (param1[(_loc_6 + 1)] != undefined)
                                    {
                                        _loc_5 = param1[(_loc_6 + 1)].@label.substring(2, 4);
                                    }
                                    else
                                    {
                                        _loc_5 = param1[_loc_6].@label.substring(2, 4);
                                    }
                                }
                                else
                                {
                                    _loc_5 = param1[_loc_6].@label.substring(2, 4);
                                }
                                navItem = new NavItem( _loc_5, "", btnsArray.length);
                                navItem.name = "navItem" + btnsArray.length;
                                navItem.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
                                navItem.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
                                navItem.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                                addChild(navItem);
                                btnsArray.push(navItem);
                            }
                        }
                        _loc_7++;
                    }
                    if (_loc_2 == 36)
                    {
                    }
                    else if (_loc_2 == 1)
                    {
                    }
                    else
                    {
                        _loc_2 = _loc_2 + 1;
                    }
                    if (_loc_2 == 71)
                    {
                        _loc_2 = 1;
                        _loc_5 = _loc_5 + (" - " + param1[_loc_6].@label.substring(2, 4));
                        btnsArray[(btnsArray.length - 1)].field.text = _loc_5;
                        if (param1[(_loc_6 + 1)] != undefined)
                        {
                            _loc_5 = param1[(_loc_6 + 1)].@label.substring(2, 4);
                        }
                        else
                        {
                            _loc_5 = param1[_loc_6].@label.substring(2, 4);
                        }
                        navItem = new NavItem( _loc_5, "", btnsArray.length);
                        navItem.name = "navItem" + btnsArray.length;
                        navItem.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
                        navItem.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
                        navItem.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                        addChild(navItem);
                        btnsArray.push(navItem);
                    }
                }
                _loc_6++;
            }
            if (_loc_5.length == 2)
            {
                _loc_5 = _loc_5 + (" - " + _loc_4);
                btnsArray[(btnsArray.length - 1)].field.text = _loc_5;
                if (_loc_2 == 1)
                {
                    removeChild(btnsArray[(btnsArray.length - 1)]);
                    btnsArray.pop();
                }
            }
            return;
        }

        public function fadeInShellAuto(param1:int) : void
        {
            if (btnsArray.length > 1)
            {
                visible = true;
            }
            else
            {
                visible = false;
            }
            TweenMax.to(this, Constant.fadeSpeed, {alpha:1, overwrite:true, delay:Constant.fadeSpeed});
            switchColsAuto(param1);
            return;
        }

        function switchCols(param1:Number) : void
        {
            fadeBtns();
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function btnRollOver(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.overColor, overwrite:true});
            }
            else
            {
                TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
            }
            return;
        }

        function toLowercase(param1:String) : String
        {
            var _loc_2:String = null;
            var _loc_3:* = undefined;
            _loc_2 = "";
            _loc_3 = 0;
            while (_loc_3 < param1.length)
            {
                
                if (param1.charAt(_loc_3) == " ")
                {
                    _loc_2 = _loc_2 + "-";
                }
                else
                {
                    _loc_2 = _loc_2 + param1.charAt(_loc_3).toLowerCase();
                }
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }

        function fadeBtns() : void
        {
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < btnsArray.length)
            {
                
                if (btnsArray[_loc_1].idNum != currNavId)
                {
                    TweenMax.to(btnsArray[_loc_1].field, Constant.fadeSpeed, {tint:Constant.coldColor, overwrite:true});
                }
                else
                {
                    TweenMax.to(btnsArray[_loc_1].field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
                }
                _loc_1++;
            }
            return;
        }

        function btnRollOut(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.coldColor, overwrite:true});
            }
            return;
        }

        function switchColsAuto(param1:Number) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            _loc_2 = btnsArray.length;
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                if (toLowercase(btnsArray[_loc_3].idNum) == toLowercase(PathManager.pathNames[param1]))
                {
                    currNavId = btnsArray[_loc_3].idNum;
                    TweenMax.to(btnsArray[_loc_3].field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
                }
                else
                {
                    TweenMax.to(btnsArray[_loc_3].field, Constant.fadeSpeed, {tint:Constant.coldColor, overwrite:true});
                }
                _loc_3++;
            }
            return;
        }

        function btnClick(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
            currNavId = event.currentTarget.idNum;
            switchCols(event.currentTarget.idNum);
            updateAddress();
            return;
        }

        function updateAddress() : void
        {
            switch(PathManager.pathNames[1])
            {
                case "architecture":
                {
                    PathManager.clickPathNames = ["projects", "architecture", "date", toLowercase(this.name), currNavId];
                    break;
                }
                case "products":
                {
                    PathManager.clickPathNames = ["projects", "products", "date", currNavId];
                    break;
                }
                case "exhibitions":
                {
                    PathManager.clickPathNames = ["projects", "exhibitions", "date", currNavId];
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        public function fadeInShell() : void
        {
            if (initShell == false)
            {
                initShell = true;
                currNavId = 0;
            }
            switchCols(0);
            updateAddress();
            if (btnsArray.length > 1)
            {
                visible = true;
            }
            else
            {
                visible = false;
            }
            TweenMax.to(this, Constant.fadeSpeed, {alpha:1, overwrite:true, delay:Constant.fadeSpeed});
            return;
        }

    }
}
